package org.javaqq.face.main.base;

import java.awt.Component;
import java.awt.Color;
import javax.swing.*;
import javax.swing.plaf.UIResource;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.*;

import org.javaqq.face.util.JavaQQColor;

import sun.swing.DefaultLookup;

public class DefaultTableHeaderRenderer extends DefaultTableCellRenderer  implements UIResource {

	private boolean horizontalTextPositionSet;
	
	private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1,
			1);
	private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1,
			1, 1);
     
        public DefaultTableHeaderRenderer() {
             setHorizontalAlignment(JLabel.CENTER);
         }
     
        public void setHorizontalTextPosition(int textPosition) {
             horizontalTextPositionSet = true;
             super.setHorizontalTextPosition(textPosition);
         }
     
         public Component getTableCellRendererComponent(JTable table, Object value,
                 boolean isSelected, boolean hasFocus, int row, int column) {
             Icon sortIcon = null;
     
             boolean isPaintingForPrint = false;
     
             if (table != null) {
                 JTableHeader header = table.getTableHeader();
                 if (header != null) {
                     Color fgColor = null;
                     Color bgColor = null;
                     if (hasFocus) {
                         fgColor = JavaQQColor.getColor();
                         bgColor = JavaQQColor.getDeepLineColor(150);
                     }
                     if (fgColor == null) {
                         fgColor = JavaQQColor.getColor();
                    }
                     if (bgColor == null) {
                         bgColor = JavaQQColor.getDeepLineColor(150);
                   }
                    setForeground(fgColor);
                    setBackground(bgColor);
     
                     setFont(header.getFont());
    
                    isPaintingForPrint = header.isPaintingForPrint();
                }
    
                if (!isPaintingForPrint && table.getRowSorter() != null) {
                    if (!horizontalTextPositionSet) {
                        // There is a row sorter, and the developer hasn't
                        // set a text position, change to leading.
                        setHorizontalTextPosition(JLabel.LEADING);
                    }
                    /*SortOrder sortOrder = getColumnSortOrder(table, column);
                    if (sortOrder != null) {
                        switch(sortOrder) {
                        case ASCENDING:
                            sortIcon = UIManager.getIcon(
                                 "Table.ascendingSortIcon");
                            break;
                        case DESCENDING:
                            sortIcon = UIManager.getIcon(
                                "Table.descendingSortIcon");
                             break;
                        case UNSORTED:
                            sortIcon = UIManager.getIcon(
                                "Table.naturalSortIcon");
                            break;
                        }
                   }*/
               }
            }
    
            setText(value == null ? "" : value.toString());
            //setIcon(sortIcon);
  
         //Border border = null;
           /*if (hasFocus) {
               border = UIManager.getBorder("TableHeader.focusCellBorder");
           }
           if (border == null) {
               border = UIManager.getBorder("TableHeader.cellBorder");
           }*/
            if(column!=0){
            	setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, table.getGridColor()));
            }else{
            	setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, table.getGridColor()));
            }
   
           return this;
       }
   
       public static SortOrder getColumnSortOrder(JTable table, int column) {
           SortOrder rv = null;
           if (table.getRowSorter() == null) {
               return rv;
           }
           java.util.List<? extends RowSorter.SortKey> sortKeys =
               table.getRowSorter().getSortKeys();
            if (sortKeys.size() > 0 && sortKeys.get(0).getColumn() ==
                table.convertColumnIndexToModel(column)) {
                rv = sortKeys.get(0).getSortOrder();
           }
            return rv;
       }

}
